import { Component, OnInit } from '@angular/core';

import { InitService } from 'app/public/serve/init/init.service';
import { HintsService } from 'app/public/serve/hints/hints.service';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { RemoveTableService } from 'app/public/serve/remove-table/remove-table.service';
import { Service } from '../../../server/server';

import { sysconfig } from '../sysconfig.data';
import { sysconfigUrl } from '../sysconfig.url';

declare var $:any;

@Component({
  selector: 'sysconfig-table',
  templateUrl: './sysconfig-table.component.html',
  styleUrls: ['./sysconfig-table.component.css']
})

export class SysconfigTableComponent implements OnInit {

  tabVal     : Array<object>;
  total      : number = 0;
  naturalNum : number = 0;

  isSelectAll : boolean = false;
  isChose     : boolean = false;
  
  right: any = { "system" : {}, "sysconfig" : {} };
  constructor(
    public initService: InitService,
    private request: CudTableService,
    private removeTableService: RemoveTableService,
    private hintsService: HintsService,
    private server: Service
  ) {
    this.right = $.extend(this.right, server.getRight());
  }

  ngOnInit() {   
    this._table()
  }

  /**
   * 表格
   */ 
  _table() {
    this.request
        .postRequest(this.server.baseUrl +sysconfigUrl.getList, this.tableData())
        .then((res: any)=>{
          if (res.result_code == '0000') {
            this.tabVal = res.result;
            this.total = res.length;
            this.naturalNum = sysconfig.pagination.naturalNum;
          } else {
            this.hintsService.messages(res.message, res.result_code);
          }
          this._cancelChecked();
          return
        })
        .catch((err: any)=> {
          this.hintsService.messages('error', 'error');
          this._cancelChecked();
          return
        });
  }

  /**
   * 全选
   * @param bool 
   * @param tabVal 
   */ 
  _selectAll(bool, tabVal) {  
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'sysConfigId');
    if (this.isSelectAll == false) {
      this._table();
    }
  }

  /**
   * 单选（多选）
   * @param bool 
   * @param sysConfigId 
   * @param length 
   */ 
  _chose(bool, sysConfigId, length) {  
    this.isSelectAll = this.removeTableService.removeChose(bool, sysConfigId, length)['bool'];
  }

  /**
   * 重置表格复选框、选中的ID
   */
  private _cancelChecked() {
    this.isChose = this.isSelectAll = false;
    this.removeTableService._clearId('sysConfigId');
  }

  /************************ 提交给后端的数据 ************************/

  private tableData() {
    return {
      id            : '0',
      pageNumber    : sysconfig.pagination.pageNumber,
      pageSize      : sysconfig.pagination.pageSize,
      queryType     : sysconfig.retrieval.queryType,
      queryCriteria : sysconfig.retrieval.queryCriteria
    }
  }

}
